﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sudoku
{
    public class SudokuBoardBuilder : ISudokuProvider
    {
        private int size;
        private SquareBox kutija;
        public SudokuBoardBuilder()
        {
                
        }
        private void LoadProviderFromConfig(int size)
        {
            this.size = size;
        }
        public SquareBox CreateSudokuBoard(int value)
        {
            if(value == 9)
            {
                DAL.DAL d = DAL.DAL.Instanca;
                d.kreirajKonekciju("127.0.0.1", "sudoku", "root", "");
                DAL.DAL.SquareBoxDAO tDao = new DAL.DAL.SquareBoxDAO();
                this.kutija = tDao.getById(1);
                d.terminirajKonekciju();

                this.kutija.popuniTablu();
                return this.kutija;
            }
            return null;
            
        }
        public void NewGame(int value)
        {
            int inside = 0, outside = 9;
            if(value == 0) //Level begginer
            {
                inside = 8;
            }
            else if(value == 1)//Level amateur
            {
                inside = 6;
            }
            else if(value == 2)//Level professional
            {
                inside = 4;
                outside = 6;
            }
            else if(value == 3)//Level master
            {
                inside = 3;
                outside = 4;
            }

            int x, y;
            Random rand = new Random();

            List<Int32> vanjski = new List<Int32>();
            for (Int32 i = 0; i < outside; i++)
            {
                Int32 curValue = rand.Next(1, 10);
                while (vanjski.Exists(val => val == curValue))
                {
                    curValue = rand.Next(1, 10);
                }
                vanjski.Add(curValue);
            }
            List<Int32> unutarnji = new List<Int32>();

            for (int i = 0; i < outside; i++)
            {
                unutarnji.Clear();
                for (Int32 count = 0; count < inside; count++)
                {
                    Int32 curValue = rand.Next(1, 10);
                    while (unutarnji.Exists(val => val == curValue))
                    {
                        curValue = rand.Next(1, 10);
                    }
                    unutarnji.Add(curValue);
                }

                x = vanjski[i];
                for (int j = 0; j < inside; j++)
                {
                    y = unutarnji[j];
                    RichTextBox txt = this.kutija.combox[i].Boxes[j].Txt;
                    txt.ReadOnly = true;
                    txt.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
                    txt.Text = this.kutija.combox[i].Boxes[j].OriginalDigit.ToString();
                    this.kutija.combox[i].Boxes[j].Editable = false;
                }
            }
        }
        public int ValueAt(int x, int y)
        {
            return this.kutija.combox[x].Boxes[y].EnteredDigit;
        }
        public bool isValueHidden(int x, int y)
        {
            return !this.kutija.combox[x].Boxes[y].Editable;
        }
    }
}
